// Douglas Kirby

#include "food.h"
#include <QApplication>
#include <QLabel>
#include <QLineEdit>
#include <QTextBrowser>
#include <QProgressBar>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QIntValidator>
#include <QPushButton>
#include <QDebug>

Food::Food()
{
    QVBoxLayout* MainLayoutPtr = new QVBoxLayout(this);
    QHBoxLayout* LabelLayoutPtr = new QHBoxLayout;
    QGridLayout* BoxLayoutPtr = new QGridLayout;
    QHBoxLayout* OverallLayoutPtr = new QHBoxLayout;
    QHBoxLayout* TextLayoutPtr = new QHBoxLayout;
    QHBoxLayout* ButtonLayoutPtr = new QHBoxLayout;

    MainLayoutPtr->addLayout(LabelLayoutPtr);
    MainLayoutPtr->addStretch();
    MainLayoutPtr->addLayout(BoxLayoutPtr);
    MainLayoutPtr->addStretch();
    MainLayoutPtr->addLayout(OverallLayoutPtr);
    MainLayoutPtr->addStretch();
    MainLayoutPtr->addLayout(TextLayoutPtr);
    MainLayoutPtr->addStretch();
    MainLayoutPtr->addLayout(ButtonLayoutPtr);

    QLabel* TitlePtr = new QLabel(tr("Food Critic"));

    LabelLayoutPtr->addStretch();
    LabelLayoutPtr->addWidget(TitlePtr);
    LabelLayoutPtr->addStretch();

    QLabel* TasteTextPtr = new QLabel(tr("Taste"));
    QLabel* PresentationTextPtr = new QLabel(tr("Presentation"));
    QLabel* AtmosphereTextPtr = new QLabel(tr("Atmosphere"));
    QLabel* ServiceTextPtr = new QLabel(tr("Service"));
    QLabel* CostTextPtr = new QLabel(tr("Cost"));

    TastePtr = new QLineEdit;
    QIntValidator* TasteValidator = new QIntValidator(0, 5, TastePtr);
    TastePtr->setValidator(TasteValidator);

    PresentationPtr = new QLineEdit;
    QIntValidator* PresentationValidator = new QIntValidator(0, 5, PresentationPtr);
    PresentationPtr->setValidator(PresentationValidator);

    AtmospherePtr = new QLineEdit;
    QIntValidator* AtmosphereValidator = new QIntValidator(0, 5, AtmospherePtr);
    AtmospherePtr->setValidator(AtmosphereValidator);

    ServicePtr = new QLineEdit;
    QIntValidator* ServiceValidator = new QIntValidator(0, 5, ServicePtr);
    ServicePtr->setValidator(ServiceValidator);

    CostPtr = new QLineEdit;
    QIntValidator* CostValidator = new QIntValidator(0, 5, CostPtr);
    CostPtr->setValidator(CostValidator);

    TasteProgressPtr = new QProgressBar;
    TasteProgressPtr->setMaximum(5);
    PresentationProgressPtr = new QProgressBar;
    PresentationProgressPtr->setMaximum(5);
    AtmosphereProgressPtr = new QProgressBar;
    AtmosphereProgressPtr->setMaximum(5);
    ServiceProgressPtr = new QProgressBar;
    ServiceProgressPtr->setMaximum(5);
    CostProgressPtr = new QProgressBar;
    CostProgressPtr->setMaximum(5);

    BoxLayoutPtr->addWidget(TasteTextPtr,0,0);
    BoxLayoutPtr->addWidget(TastePtr,0,1);
    BoxLayoutPtr->addWidget(TasteProgressPtr,0,2);
    BoxLayoutPtr->addWidget(PresentationTextPtr,1,0);
    BoxLayoutPtr->addWidget(PresentationPtr,1,1);
    BoxLayoutPtr->addWidget(PresentationProgressPtr,1,2);
    BoxLayoutPtr->addWidget(AtmosphereTextPtr,2,0);
    BoxLayoutPtr->addWidget(AtmosphereProgressPtr,2,2);
    BoxLayoutPtr->addWidget(AtmospherePtr,2,1);
    BoxLayoutPtr->addWidget(ServiceTextPtr,3,0);
    BoxLayoutPtr->addWidget(ServicePtr,3,1);
    BoxLayoutPtr->addWidget(ServiceProgressPtr,3,2);
    BoxLayoutPtr->addWidget(CostTextPtr,4,0);
    BoxLayoutPtr->addWidget(CostPtr,4,1);
    BoxLayoutPtr->addWidget(CostProgressPtr,4,2);

    QLabel* OverallTextPtr = new QLabel(tr("Overall Dining Experience"));
    OverallPtr = new QLineEdit;
    OverallPtr->setReadOnly(true);

    OverallLayoutPtr->addStretch();
    OverallLayoutPtr->addWidget(OverallTextPtr);
    OverallLayoutPtr->addWidget(OverallPtr);
    OverallLayoutPtr->addStretch();

    InstructionPtr = new QTextBrowser;
    InstructionPtr->setHtml("Directions:<br><br>"
                            "Enter an integer from <b>0</b> (Bad) to <b>5</b> (Good) "
                            "for the five categories (Taste, Presentation, Atmosphere, "
                            "Service, and Cost) and the Overall Dining Experience will "
                            "be computed as the average of the five inputs.  An audible "
                            "<b>beep</b> indicates the you have attempted to input a non-integer "
                            "or an integer outside of the allowable range. "
                            "Progress bars will provide feedback.<br><br>"
                            "Use <b>Clear</b> to start over or <b>Quit</b> to terminate the application.");

    TextLayoutPtr->addWidget(InstructionPtr);

    ClearButtonPtr = new QPushButton(tr("Clear"));
    ClearButtonPtr->setDefault(true);
    QuitButtonPtr =new QPushButton(tr("Quit"));

    ButtonLayoutPtr->addWidget(ClearButtonPtr);
    ButtonLayoutPtr->addStretch();
    ButtonLayoutPtr->addWidget(QuitButtonPtr);

    connect(QuitButtonPtr, SIGNAL(clicked()), this, SLOT(accept()));
    connect(ClearButtonPtr, SIGNAL(clicked()), this, SLOT(ClearText()));
    connect(TastePtr, SIGNAL(textChanged(QString)), this, SLOT(TasteChanged(QString)));
    connect(PresentationPtr, SIGNAL(textChanged(QString)), this, SLOT(PresentationChanged(QString)));
    connect(AtmospherePtr,SIGNAL(textChanged(QString)), this, SLOT(AtmosphereChanged(QString)));
    connect(ServicePtr,SIGNAL(textChanged(QString)), this, SLOT(ServiceChanged(QString)));
    connect(CostPtr, SIGNAL(textChanged(QString)), this, SLOT(CostChanged(QString)));

}

    void Food::TasteChanged(const QString& value)
    {
        QString TempStr = value;
        int TempNum = TempStr.toInt();
        OverallUpdate();
        TasteProgressPtr->setValue(TempNum);
    }

    void Food::PresentationChanged(const QString& value)
    {
        QString TempStr = value;
        int TempNum = TempStr.toInt();
        OverallUpdate();
        PresentationProgressPtr->setValue(TempNum);
    }

    void Food::AtmosphereChanged(const QString& value)
    {
        QString TempStr = value;
        int TempNum = TempStr.toInt();
        OverallUpdate();
        AtmosphereProgressPtr->setValue(TempNum);
    }

    void Food::ServiceChanged(const QString& value)
    {
        QString TempStr = value;
        int TempNum = TempStr.toInt();
        OverallUpdate();
        ServiceProgressPtr->setValue(TempNum);
    }

    void Food::CostChanged(const QString& value)
    {
        QString TempStr = value;
        int TempNum = TempStr.toInt();
        OverallUpdate();
        CostProgressPtr->setValue(TempNum);
    }

    void Food::OverallUpdate()
    {
        float Num = float(TastePtr->text().toInt() + PresentationPtr->text().toInt() + AtmospherePtr->text().toInt() + ServicePtr->text().toInt() + CostPtr->text().toInt())/5.0;
        QString Total = QString("");
        Total.setNum(Num, 'g', 4);
        OverallPtr->setText(Total);
    }

    void Food::ClearText()
    {
        TastePtr->setText("");
        PresentationPtr->setText("");
        AtmospherePtr->setText("");
        ServicePtr->setText("");
        CostPtr->setText("");
    }
